const app = getApp();
import {request} from '../../utils/request'
import config from '../../utils/config'

Page({
    data: {
        custom:{},
        adList:[],
        skuList:[{sku_name:"品牌",id:0}],
        sku_id:0,
        index:1,
        skuDetailList:[],
        picUrl:config.picUrl,
        brandList:[
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
            {id:'',shop_name:"时空裂痕",brand_avatar:'/static/img/brand.png',view:232,fans:232,thumb_up:234,sales_num:4323},
        ],
        page:1,
        last_page:1,
        endHide:true
    },
    onLoad(options) {
        wx.showShareMenu({
            menus: ["shareAppMessage","shareTimeline"],
          })
        this.setData({
            custom:wx.getMenuButtonBoundingClientRect()
        })
        this.getTop()
        this.getSku()
    },
    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 1
            })
        }
    },
    getTop(){
        request({url:'/miniprogram/system/getTop',type:'get'}).then(res=>{
            this.setData({adList:res.data})
        })
    },
    getSku(){
        request({url:"/miniprogram/system/getSkuList"}).then(res=>{
            if(res.code != 200){
                return false
            }
            this.setData({
                skuList:this.data.skuList.concat(res.data),
                sku_id:res.data[this.data.index].id
            })
            this.getSkuDetailList(res.data[0].id)
        })
    },
    touchSku(e){
        var {index,id} = e.currentTarget.dataset
        this.setData({index:index})
        if(index > 0){
            this.getSkuDetailList(id)
        }else{
            this.getPublisherList()
        }
    },
    getSkuDetailList(sku_id){
        wx.showLoading()
        request({url:"/miniprogram/system/getSkuDetailList",data:{sku_id},type:'post'}).then(res=>{
            wx.hideLoading()
            this.setData({skuDetailList:res.data})
        })
    },
    getPublisherList(){
        request({url:"/miniprogram/user/getPublisherList",data:{page:this.data.page}}).then(res=>{
            if(this.data.page == 1) this.setData({brandList:[]})
            this.setData({
                brandList:this.data.brandList.concat(res.data),
                last_page:res.last_page
            })
        })
    },
    getBrand(){
        console.log(123)
        if(this.data.page >= this.data.last_page){
            this.setData({endHide:false})
            return false;
        }
        this.setData({page:this.data.page+1})
        this.getPublisherList()
    },
    onPullDownRefresh() {

    },
    onReachBottom() {

    },
    onShareAppMessage() {

    }
})